﻿// Author: Prasanna V. Loganathar
// Project: Edi.Android
// 
// Copyright 2014 Launchark. All Rights Reserved.
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//  
// Created: 5:25 AM 19-04-2014

namespace Edi.Android
{
    using System.Reflection;
    using System.Threading.Tasks;

    using Edi.Common.Bootstrap;

    using global::Android.App;
    using global::Android.Content.PM;
    using global::Android.OS;
    using global::Android.Views;
    using global::Android.Webkit;

    [Activity(Label = "Edi", MainLauncher = true, ScreenOrientation = ScreenOrientation.Portrait)]
    public class MainActivity : Activity
    {
        protected override async void OnCreate(Bundle savedInstanceState)
        {
            Window.RequestFeature(WindowFeatures.NoTitle);
            SetContentView(Resource.Layout.Splash);

            base.OnCreate(savedInstanceState);

            // Deliberate await on a method with 'void' return.
            // To trigger UI render, and then wait until continuation completes.

            await Task.Run(async () => await InitializeAsync());

            // UI state is already frozen on splash screen. So, not necessary to queue to the threadpool.
            // Run synchronously.

            var indexData = GetIndexHtml();

            SetContentView(Resource.Layout.Main);

            var webView = FindViewById<WebView>(Resource.Id.webView);
            webView.Settings.JavaScriptEnabled = true;
            webView.SetWebViewClient(new WebUi(this));
            webView.LoadData(indexData, "text/html", "UTF-8");
        }

        private async Task InitializeAsync()
        {
            Global.EntryAssembly = Assembly.GetEntryAssembly();
            var startup = Global.Container.GetInstance<Startup>();
        }

        private string GetIndexHtml()
        {
            return "Hello";
        }
    }
}